package com.yf1706.service;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yf1706.bean.Salary;
import com.yf1706.bean.SalarySob;
import com.yf1706.dao.jpa.ISalaryJDAO;
import com.yf1706.dao.jpa.ISalarySobJDAO;
/**
 * 薪资服务
 * @author 锦涛
 *
 */

@Service
@Transactional(rollbackFor=Exception.class)
public class SalaryService {

	@Autowired
	private ISalarySobJDAO salarySobDAO;
	
	@Autowired
	private ISalaryJDAO salaryDAO;
	
	//----------------薪资账套--------------------
	/**
	 * 添加薪资账套
	 */
	@Transactional
	public void saveSob(SalarySob salarySob) {
		salarySobDAO.save(salarySob);
	}
	
	/**
	 * 删除薪资账套
	 */
	@Transactional
	public void removeSob(String ids) {
		for(String id : ids.split(",") ) {
			if ( id != null) {
				salarySobDAO.deleteById(Integer.parseInt(id));
			}
		}
	}
	
	/**
	 * 修改薪资账套
	 */
	@Transactional
	public void updateSob(SalarySob salarySob) {
		Optional<SalarySob> SsalarySob = salarySobDAO.findById(salarySob.getSalarySobId());
		SsalarySob.get().setSalarySobName(salarySob.getSalarySobName());
		SsalarySob.get().setRemark(salarySob.getRemark());
	}
	
	/**
	 * 分页查询薪资账套
	 */
	@Transactional
	public Page<SalarySob> querySobByPage(int page,int rows,String sort,String order){
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return salarySobDAO.findAll(pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return salarySobDAO.findAll(pageable);
		}
	}
	
	/**
	 * 模糊分页查询薪资账套
	 */
	@Transactional
	public Page<SalarySob> findSearchSob(String salarySobName,int page,int rows,String sort,String order) {
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return salarySobDAO.findSearch(salarySobName, pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return salarySobDAO.findSearch(salarySobName, pageable);
		}
	}
	
	/**
	 * 查询所有的薪资账套
	 */
	public List<SalarySob> querySobAll(){
		return salarySobDAO.findAll();
	}
	
	/**
	 * 通过ID查询薪资账套
	 */
	public SalarySob findSobByID(int id) {
		return salarySobDAO.getOne(id);
	}
	
	//----------------薪资表---------------------
	/**
	 * 添加薪资表
	 */
	@Transactional
	public void saveS(Salary salary) {
		salaryDAO.save(salary);
		Optional<SalarySob> SbaseSalarySob = salarySobDAO.findById(salaryDAO.getOne(salary.getSalaryId()).getSalarySob().getSalarySobId());
		SbaseSalarySob.get().setSalaryNum(SbaseSalarySob.get().getSalaryNum()+1);
	}
	
	/**
	 * 删除薪资表
	 */
	@Transactional
	public void removeS(String ids) {
		for(String id : ids.split(",") ) {
			if ( id != null) {
				salaryDAO.deleteById(Integer.parseInt(id));
				Optional<SalarySob> SbaseSalarySob = salarySobDAO.findById(salaryDAO.getOne(Integer.parseInt(id)).getSalarySob().getSalarySobId());
				SbaseSalarySob.get().setSalaryNum(SbaseSalarySob.get().getSalaryNum()-1);
			}
		}
		
	}
	
	/**
	 * 修改薪资表
	 */
	@Transactional
	public void updateS(Salary salary) {
		Optional<Salary> Ssalary = salaryDAO.findById(salary.getSalaryId());
		Ssalary.get().setSalaryName(salary.getSalaryName());
		Ssalary.get().setCountStatus(salary.getCountStatus());
		Ssalary.get().setApprover(salary.getApprover());
		Ssalary.get().setApproveDate(salary.getApproveDate());
		Ssalary.get().setApproveStatus(salary.getApproveStatus());
		Ssalary.get().setSalaryBeginDate(salary.getSalaryBeginDate());
		Ssalary.get().setSalaryOffDate(salary.getSalaryOffDate());
		Ssalary.get().setSalaryTotal(salary.getSalaryTotal());
		Ssalary.get().setRemark(salary.getRemark());
		
	}
	
	/**
	 * 分页查询薪资表
	 */
	@Transactional
	public Page<Salary> querySByPage(int id,int page,int rows,String sort,String order){
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return salaryDAO.findSearch(id, pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return salaryDAO.findSearch(id, pageable);
		}
	}
	
	/**
	 * 查询所有薪资表
	 */
	public List<Salary> querySAll(){
		return salaryDAO.findAll();
	}
	
	/**
	 * 根据薪资表ID获得薪资表
	 */
	public Salary findBySalaryId(int salaryId) {
		return salaryDAO.getOne(salaryId);
	}
}
